#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include "common.h"

#define MAX_FILE_SIZE (10 * 1024 * 1024) // 10MB最大文件大小
void builder(unsigned char *data, size_t data_size,const CONFIG *cfg){
    size_t start_len = strlen(cfg->start);
    size_t end_len = strlen(cfg->end);

    char *new_conent = (char*) malloc(sizeof(cfg->host_info));
    HOST_INFO host_info = cfg->host_info;
    host_to_network_host_info(&host_info);
    memset(new_conent,0,sizeof(host_info));
    mempcpy(new_conent,(char*)&host_info,sizeof(host_info));

    size_t new_content_len = sizeof(host_info);
    unsigned char *start_pos = NULL;
    for (size_t i = 0; i <= data_size - start_len; i++) {
        if (memcmp(data + i, cfg->start, start_len) == 0) {
            start_pos = data + i + start_len;
            break;
        }
    }

    if (!start_pos) {
        fprintf(stderr, "Start key not found\n");
        return;
    }

    // 查找结束标记
    unsigned char *end_pos = NULL;
    for (size_t i = (start_pos - data); i <= data_size - end_len; i++) {
        if (memcmp(data + i, cfg->end, end_len) == 0) {
            end_pos = data + i;
            break;
        }
    }

    if (!end_pos) {
        fprintf(stderr, "End key not found\n");
        return ;
    }

    // 检查内容区域大小是否足够
    size_t content_area_size = end_pos - start_pos;
    if (new_content_len > content_area_size) {
        fprintf(stderr, "New content too large for the allocated space\n");
        return ;
    }
    memset(start_pos+1, 0, new_content_len); // 清空原有内容
    memcpy(start_pos+1, new_conent, new_content_len);
}

// 读取文件到内存
unsigned char *read_file(const char *filename, size_t *file_size) {
    FILE *file = fopen(filename, "rb");
    if (!file) {
        perror("Failed to open file");
        return NULL;
    }

    fseek(file, 0, SEEK_END);
    *file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    if (*file_size > MAX_FILE_SIZE) {
        fprintf(stderr, "File too large\n");
        fclose(file);
        return NULL;
    }

    unsigned char *buffer =( unsigned char *) malloc(*file_size);
    if (!buffer) {
        perror("Memory allocation failed");
        fclose(file);
        return NULL;
    }

    if (fread(buffer, 1, *file_size, file) != *file_size) {
        perror("Failed to read file");
        free(buffer);
        fclose(file);
        return NULL;
    }

    fclose(file);
    return buffer;
}

// 写入文件
int write_file(const char *filename, const unsigned char *data, size_t size) {
    FILE *file = fopen(filename, "wb");
    if (!file) {
        perror("Failed to open file for writing");
        return -1;
    }

    if (fwrite(data, 1, size, file) != size) {
        perror("Failed to write file");
        fclose(file);
        return -1;
    }

    fclose(file);
    return 0;
}
int main(int argc, char *argv[])
{

    size_t fileSize ;
    unsigned char * buffer = read_file("agent.exe",&fileSize);
    CONFIG config ={START_KEY,{"",0,0},END_KEY};
    memcpy(config.host_info.address,"192.168.201.101",sizeof("192.168.201.101"));
    config.host_info.port = 8080;
    config.host_info.protocol = 1;

    builder(buffer,fileSize,&config);
    write_file("agent_new.exe",buffer,fileSize);
    free(buffer);
    return 0;
}
